package objects.base;

import java.util.Date;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

public class WordData {
	private String word;
	private String realWord;
	private int sum;
	private double mean;
	private double sigma;
	private double lastFrequency;
	private Hashtable<Date, Set<Document>> hashWord;
	private Hashtable<String, Double> hashfrequencies;
	private Hashtable<String, Double> hashZscore;
	
	public static Hashtable<String, Set<String>> hashTrendDate = new Hashtable<String, Set<String>>();
	
	public WordData(String Word){
		this.setWord(Word.toLowerCase());
		this.realWord = utils.primitif.StringUtils.removeBadChar(Word.toLowerCase());
		this.setSum(0);
		this.setMean(0);
		this.setSigma(0);
		hashWord = new Hashtable<Date, Set<Document>>();
		setHashfrequencies(new Hashtable<String, Double>());
		hashZscore = new Hashtable<String, Double>();
		
	}

	public void put(Date date, Document document){
		Set<Document> temp;
		if(hashWord.containsKey(date)){
			temp = hashWord.get(date);
		}else{
			temp = new HashSet<Document>();
		}
		if(!temp.contains(document)){
			temp.add(document);
		}
		
		hashWord.put(date, temp);
	}
	public static void puthashTrendDate(String date, String word){
		Set<String> temp;
		if(hashTrendDate.containsKey(date)){
			temp = hashTrendDate.get(date);
		}else{
			temp = new HashSet<String>();
		}
		if(!temp.contains(word)){
			temp.add(word);
		}
		hashTrendDate.put(date, temp);
	}
	public void putFrequencies(Date date, double frenquency){
		hashfrequencies.put(date.toString(), frenquency);
	}
	public void putZscore(Date date, double zscore){
		hashZscore.put(date.toString(), zscore);
	}
	
	public double[] frequenciesDoubleArrayFromHash(){
		double[] tab = new double[hashfrequencies.values().toArray().length];
		int i = 0;
		for(Object o: hashfrequencies.values().toArray()){
			tab[i++] = (Double) o;
		}
		return tab;
	}
	
	public Hashtable<Date, Set<Document>> getHashWord(){
		return hashWord;
	}

	public void setWord(String word) {
		this.word = word;
	}

	public String getWord() {
		return word;
	}
	
	public String getRealWord() {
		return realWord;
	}
	
	public void setHashfrequencies(Hashtable<String, Double> hashfrequencies) {
		this.hashfrequencies = hashfrequencies;
	}

	public Hashtable<String, Double> getHashfrequencies() {
		return hashfrequencies;
	}

	public void setSum(int sum) {
		this.sum = sum;
	}

	public int getSum() {
		return sum;
	}

	public void setMean(double mean) {
		this.mean = mean;
	}

	public double getMean() {
		return mean;
	}

	public void setSigma(double sigma) {
		this.sigma = sigma;
	}

	public double getSigma() {
		return sigma;
	}

	public String toString(){
		StringBuffer sBuff = new StringBuffer();
		sBuff.append("->");
		sBuff.append(this.word);
		sBuff.append("\n");
		sBuff.append("[");
		Iterator<Set<Document>> itValue = this.hashWord.values().iterator(); 
		Iterator<Date> itKey = this.hashWord.keySet().iterator();

		while(itKey.hasNext()){ 
			Set<Document> value = itValue.next();
			Date d = itKey.next();
			sBuff.append("\n");
			sBuff.append(d.toString());
			sBuff.append("\n");
			sBuff.append("[");
			Iterator<Document> i = value.iterator(); 
			while(i.hasNext())
			{
				sBuff.append(i.next()); 
				sBuff.append(",");
			}
			sBuff.append("]");
			sBuff.append("\n");
		} 
		sBuff.append("]");
		return sBuff.toString();
	}

	public void setHashZscore(Hashtable<String, Double> hashZscore) {
		this.hashZscore = hashZscore;
	}

	public Hashtable<String, Double> getHashZscore() {
		return hashZscore;
	}

	public void setLastFrequency(double lastFrequency) {
		this.lastFrequency = lastFrequency;
	}

	public double getLastFrequency() {
		return lastFrequency;
	}
}
